
package com.tiantian.app.manager;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DbAdapter {



    private static final String TAG = "DbAdapter";
    private static DatabaseHelper mDbHelper = null;
    private static SQLiteDatabase mDb = null;
    
    private static final int DATABASE_VERSION =1;
    private static final String DATABASE_NAME = "appOn.a001.DB"+DATABASE_VERSION;
     
    
   
    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            initDatabase(db);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            try {

//                Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
//                if (c != null) {
//                    while(c.moveToNext()) {
//                        String name = c.getString(c.getColumnIndexOrThrow("name"));
//                        
//                        db.execSQL("DROP TABLE IF EXISTS " + name + ";");
//                    }
//                }
//
//
//                try {
//                    c.close();
//                    c = null;
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                
                onCreate(db);
                
            
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        

        
        private void initDatabase(SQLiteDatabase db) {
//            db.execSQL(ProjDbAdapter.TABLE_WEBVERSION_SETTINGS);
            db.execSQL(ProjDbAdapter.TABLE_APPINFOS_SETTING);
//            db.execSQL(ProjDbAdapter.TABLE_DOWNLOAD_DATA_SETTING);
//            db.execSQL(ProjDbAdapter.TABLE_CATEGORY_VERSION_SETTING);
//            db.execSQL(ProjDbAdapter.TABLE_CATEGORY_DETAIL_SETTING);
//            db.execSQL(ProjDbAdapter.TABLE_CATEGORY_SUB_DETAIL_SETTING);
//            initCategoryVersion(db);
        }
        
//        private void initCategoryVersion(SQLiteDatabase db){
//        	db.execSQL("insert into "+ProjDbAdapter.K_TCATEGORY_VERSION+" ( "+ProjDbAdapter.K_CATEGORY_VER_ID+" , "+ ProjDbAdapter.K_CATEGORY_VERSION+" ) "
//        			+" values ( null , 1 )");
//        }
        
 
        
    }


    private DbAdapter() {
        
    }

    public static SQLiteDatabase getInstance(Context ctx) throws SQLException {

        if (mDbHelper == null) {
            mDbHelper = new DatabaseHelper(ctx);
        }
        open();
        return mDb;
    }
    
    public static void open(){
    	mDb = mDbHelper.getWritableDatabase();
    }
    
    
    public static void close() {
        if (mDbHelper != null) {
            try {
                mDbHelper.close();
                
            }catch (Exception e) {
                e.printStackTrace();
            } finally {
                mDbHelper = null;
            }
        }
        
        if (mDb != null) {
            try {
                if (mDb.isOpen())
                    mDb.close();
                
                
            }catch (Exception e) {
                e.printStackTrace();
            } finally {
                mDb = null;
            }

        }
        
    }

}
